# 使用官方 Node.js 镜像作为基础镜像
FROM node:19

# 安装必要的系统依赖
RUN apt-get update && apt-get install -y \
    curl \
    unzip \
    tree \
    && rm -rf /var/lib/apt/lists/*

# 设置应用目录
ENV APP_ROOT=/opt/foundry
ENV APP_DATA=${APP_ROOT}/data
ENV APP_HOME=${APP_ROOT}/app

# 创建所需的目录结构
RUN mkdir -p ${APP_DATA} ${APP_HOME} && \
    chown -R node:node ${APP_ROOT}

# 切换到非 root 用户
USER node

# 设置工作目录
WORKDIR ${APP_ROOT}

# 第一阶段：复制和准备文件
COPY --chown=node:node .env foundryvtt.zip* ./

# 第二阶段：下载（如果需要）
RUN if [ -f foundryvtt.zip ]; then \
        echo "Using local foundryvtt.zip"; \
        ls -l foundryvtt.zip; \
    elif [ -f .env ]; then \
        export $(grep -v '^#' .env | xargs); \
        if [ -n "$FOUNDRY_VTT_URL" ]; then \
            echo "Downloading from URL: $FOUNDRY_VTT_URL"; \
            curl -L -o foundryvtt.zip "$FOUNDRY_VTT_URL"; \
            ls -l foundryvtt.zip; \
        else \
            echo "Error: No local file or URL available" && exit 1; \
        fi; \
    else \
        echo "Error: Neither .env nor foundryvtt.zip found" && exit 1; \
    fi

# 第三阶段：解压和移动文件
RUN if [ -f foundryvtt.zip ]; then \
        echo "===== Checking ZIP file ====="; \
        file foundryvtt.zip; \
        echo "===== ZIP Contents ====="; \
        unzip -l foundryvtt.zip || exit 1; \
        echo "===== Extracting Files ====="; \
        unzip -d ${APP_HOME} foundryvtt.zip || exit 1; \
        echo "===== App Directory Contents ====="; \
        ls -la ${APP_HOME}; \
        echo "===== Cleaning up ====="; \
        rm foundryvtt.zip; \
        echo "===== Final Verification ====="; \
        if [ -f "${APP_HOME}/resources/app/main.js" ]; then \
            mv ${APP_HOME}/resources/app/* ${APP_HOME}/; \
            rm -rf ${APP_HOME}/resources; \
            echo "main.js found and moved to correct location"; \
            ls -l ${APP_HOME}/main.js; \
        elif [ -f "${APP_HOME}/main.js" ]; then \
            echo "main.js already in correct location"; \
            ls -l ${APP_HOME}/main.js; \
        else \
            echo "ERROR: main.js not found in expected locations"; \
            find ${APP_HOME} -type f; \
            exit 1; \
        fi \
    else \
        echo "Error: foundryvtt.zip not found after previous step" && exit 1; \
    fi

# 暴露默认的 Foundry VTT 端口
EXPOSE 30000

# 设置工作目录到应用目录
WORKDIR ${APP_HOME}

# 启动 Foundry VTT
CMD echo "===== Environment =====" && \
    echo "Working directory: $(pwd)" && \
    echo "APP_ROOT: ${APP_ROOT}" && \
    echo "APP_HOME: ${APP_HOME}" && \
    echo "APP_DATA: ${APP_DATA}" && \
    echo -e "\n===== Directory Contents =====" && \
    ls -la && \
    echo -e "\n===== Starting Foundry VTT =====" && \
    if [ -f "main.js" ]; then \
        echo "Found main.js in $(pwd)" && \
        node main.js \
            --dataPath=${APP_DATA} \
            --hostname=0.0.0.0 \
            --port=30000 \
            --noupdate; \
    else \
        echo "ERROR: main.js not found in $(pwd)" && \
        echo "Directory contents:" && \
        ls -la && \
        exit 1; \
    fi